/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.header.CallIdHeader;
import java.text.ParseException;

/**
 * The Call-ID header field uniquely identifies a particular invitation or all
 * registrations of a particular client. A single multimedia conference can give rise to
 * several calls with different Call-IDs, for example, if a user invites a
 * single individual several times to the same (long-running) conference.
 * Call-IDs are case-sensitive and are simply compared byte-by-byte.
 * <p>
 * Call-ID is generated by the combination of a random string and the
 * softphone's host name or IP address.  The combination of the To tag,
 * From tag, and Call-ID completely defines a peer-to-peer SIP relationship
 * between two users and is referred to as a dialog. It MUST be the same for
 * all requests and responses sent by either User Agent in a dialog.  It SHOULD be the
 * same in each registration from a User Agent.
 * <p>
 * All SIP User Agents must have a means to guarantee that the Call-ID header fields
 * they produce will not be inadvertently generated by any other User Agent.  Note
 * that when requests are retried after certain failure responses that solicit
 * an amendment to a request (for example, a challenge for authentication),
 * these retried requests are not considered new requests, and therefore do
 * not need new Call-ID header fields.
 * <p>
 * Use of cryptographically random identifiers in the generation of Call-IDs is
 * RECOMMENDED.  Implementations MAY use the form "localid@host".
 * <p>
 * For Example:<br>
 * <code>Call-ID: f81d4fae-7dec-11d0-a765-00a0c91e6bf6@jcp.org</code>
 *
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class CallIdHeaderImpl extends HeaderBase implements CallIdHeader {
    private String callId;

    private static final CallIdHeaderParser parser = new CallIdHeaderParser();

    public CallIdHeaderImpl() {
        super(CallIdHeader.NAME);
    }

    CallIdHeaderImpl(byte[] data, int start, int end) {
        super(CallIdHeader.NAME, data, start, end);
    }

    public Object clone() {
        final CallIdHeaderImpl header = new CallIdHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.callId = this.callId;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        return callId;
    }

    /**
     * Sets the Call-Id of the CallIdHeader. The CallId parameter uniquely
     * identifies a serious of messages within a dialogue.
     *
     * @param callId the string value of the Call-Id of this CallIdHeader.
     * @throws java.text.ParseException which signals that an error has been reached
     *                                  unexpectedly while parsing the callId value.
     */

    public void setCallId(String callId) throws ParseException {
        parse();
        this.callId = callId;
        invalidateHeaderData();
    }

    /**
     * Returns the Call-Id of CallIdHeader. The CallId parameter uniquely
     * identifies a series of messages within a dialogue.
     *
     * @return the String value of the Call-Id of this CallIdHeader.
     */

    public String getCallId() {
        parse();
        return this.callId;
    }
}
/*
 * $Log$
 */